﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Highlight;
using ObjectSearch.Configuration;
using Lucene.Net.Search;

namespace ObjectSearch
{
    public class SearchResultItem<T> : LuceneObjectSearchBase, ISearchResultItem<T>
    {
        public Document Document { get; private set; }
        public Query LuceneQuery { get; private set; }


        public SearchResultItem(Document document,Query query, IndexSettings settings, Configuration.Configuration configuration)
            : base(settings, configuration)
        {
            this.Document = document;
            this.LuceneQuery = query;
        }


        #region ISearchResultItem<T> Members

        public string GetFragement(string memberName)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter("", ""),
                new QueryScorer(LuceneQuery));

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);
            string value = this.Document.Get(f.FieldName);
            return highlighter.GetBestFragment(IndexSettings.Analyzer, f.FieldName, value);
        }

        public string[] GetFragements(string memberName, int maxNumberOfFragements)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter("", ""),
                new QueryScorer(LuceneQuery));

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);

            string value = this.Document.Get(f.FieldName);
            return highlighter.GetBestFragments(IndexSettings.Analyzer, value, maxNumberOfFragements);
        }

        public string GetHighlightedFragement(string memberName, string prefix, string postfix)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter(prefix, postfix),
                new QueryScorer(LuceneQuery));

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);

            string value = this.Document.Get(f.FieldName);
            return highlighter.GetBestFragment(IndexSettings.Analyzer, f.FieldName, value);
        }

        public string[] GetHighlightedFragements(string memberName, int maxNumberOfFragements, string prefix, string postfix)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter(prefix, postfix),
                new QueryScorer(LuceneQuery));

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);

            string value = this.Document.Get(f.FieldName);
            return highlighter.GetBestFragments(IndexSettings.Analyzer, value, maxNumberOfFragements);
        }

        /// <summary>
        /// A private field used to hold the value for Value property.
        /// </summary>
        private T _Value;
        public T Value
        {
            get
            {
                if (_Value == null)
                {
                    _Value = (T)Converter.DocumentToObjectConverter.Convert(Document, typeof(T), this.Configuration);
                }

                return _Value;
            }
        }

        #endregion

        #region ISearchResultItem<T> Members


        public string GetHighlightedValue(string memberName, string prefix, string postfix)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter(prefix, postfix),
                new QueryScorer(LuceneQuery));

            highlighter.SetTextFragmenter(new NullFragmenter());

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);

            string value = this.Document.Get(f.FieldName);

            string highlighted = highlighter.GetBestFragment(IndexSettings.Analyzer, f.FieldName, value);

            if (highlighted == null)
                return value;
            else
                return highlighted;
        }

        public string[] GetHighlightedValue(string memberName, int maxNumberOfFragments, string prefix, string postfix)
        {
            Highlighter highlighter = new Highlighter(
                new SimpleHTMLFormatter(prefix, postfix),
                new QueryScorer(LuceneQuery));

            highlighter.SetTextFragmenter(new NullFragmenter());

            FieldMetaData f = this.Configuration.FindMetaData<T>().FindByObjectMemberName(memberName);

            string value = this.Document.Get(f.FieldName);
            return highlighter.GetBestFragments(IndexSettings.Analyzer, value, maxNumberOfFragments);
        }

        #endregion
    }
}
